package com.zwps.extra.http.xxljob;

import com.zwps.extra.http.HttpException;
import com.zwps.extra.http.HttpService;
import com.zwps.extra.http.RequestLogRecord;
import com.zwps.extra.http.xxljob.model.dto.AddJobDTO;
import com.zwps.extra.http.xxljob.response.message.ResponseDataMessage;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import lombok.NoArgsConstructor;
import java.util.HashMap;

@NoArgsConstructor
public class XxlJobClient extends HttpService {

    public XxlJobClient(RequestLogRecord record) {
        super(record);
    }

    public String addJob(String appAddr, String accessToken, AddJobDTO dto) throws HttpException {
        return exceptionHandle(() -> {
            String url = appAddr + "/api/add_job";
            HttpRequest request = HttpUtil.createPost(url);
            request.header("XXL-JOB-ACCESS-TOKEN", accessToken);
            setRequestBody(request, dto);
            return requestReturnData(request, ResponseDataMessage.class, String.class);
        }, "添加任务失败");
    }

    public void removeJob(String appAddr, String accessToken, Integer jobId) throws HttpException {
        exceptionHandle(() -> {
            String url = appAddr + "/api/remove_job";
            HttpRequest request = HttpUtil.createPost(url);
            request.header("XXL-JOB-ACCESS-TOKEN", accessToken);
            HashMap<String, Integer> data = new HashMap<String, Integer>();
            data.put("jobId", jobId);
            setRequestBody(request, data);
            return requestReturnData(request, ResponseDataMessage.class, String.class);
        }, "删除任务失败");
    }

    public void startJob(String appAddr, String accessToken, Integer jobId) throws HttpException {
        exceptionHandle(() -> {
            String url = appAddr + "/api/start_job";
            HttpRequest request = HttpUtil.createPost(url);
            HashMap<String, Integer> data = new HashMap<String, Integer>();
            data.put("jobId", jobId);
            setRequestBody(request, data);
            request.header("XXL-JOB-ACCESS-TOKEN", accessToken);
            return requestSuccessed(request, ResponseDataMessage.class);
        }, "启用任务失败");
    }

    public void stopJob(String appAddr, String accessToken, Integer jobId) throws HttpException {
        exceptionHandle(() -> {
            String url = appAddr + "/api/stop_job";
            HttpRequest request = HttpUtil.createPost(url);
            HashMap<String, Integer> data = new HashMap<String, Integer>();
            data.put("jobId", jobId);
            setRequestBody(request, data);
            request.header("XXL-JOB-ACCESS-TOKEN", accessToken);
            return requestSuccessed(request, ResponseDataMessage.class);
        }, "停用任务失败");
    }

    public static void main(String[] args) throws Exception {
        String appAddr = "http://192.168.17.11:8080/xxl-job-admin";
        XxlJobClient client = new XxlJobClient();
        try {
            String accessToken = "default_token";
            AddJobDTO dto = AddJobDTO.builder().jobGroup(2).jobDesc("测试22").author("xxl").scheduleType("CRON")
                    .scheduleConf("0 00 00 01 * ?").glueType("BEAN").executorHandler("generateAnalysisReport")
                    .executorRouteStrategy("FIRST").misfireStrategy("DO_NOTHING").executorParam("12312")
                    .executorBlockStrategy("SERIAL_EXECUTION").executorTimeout(0).executorFailRetryCount(0).build();
//            String jobId = client.addJob(appAddr, accessToken, dto);
//            client.startJob(appAddr, accessToken, 9);
//            client.stopJob(appAddr, accessToken, 9);
            client.removeJob(appAddr, accessToken, 10);
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
